/**
 *
 */
package demo.tellurium.framework


import groovy.io.GroovyPrintWriter;

import java.io.PrintWriter;
import java.io.Writer;

import org.apache.commons.lang.math.RandomUtils;
import org.telluriumsource.dsl.DslContext;

import demo.util.BaseUtils;
import demo.util.Console;

/**
 * @author roger.xu
 *
 */
public abstract class BaseUIModule extends DslContext {
    private static final String MODULE_NAME = "BaseUIModule";

    protected static Console console = BaseUtils.getConsole();

    public String getModuleName() {
        return MODULE_NAME;
    }

    public abstract void defineUi();

    public enum OfferingTypeOptionIndex {
        NULL, OFFERING_TYPE_SWG, OFFERING_TYPE_STG, OFFERING_TYPE_STACK, OFFERING_TYPE_COMPONENT
    }

    protected static String getTimestamp() {
        return BaseUtils.getTimestamp();
    }

    protected static final int RANDOM_INDEX = -1;


    /**
     * @param widgetUid
     * @param input
     */
    protected void doInputTextBox(String widgetUid, String input) {
        onWidget(widgetUid, "inputText", input);
        pause 500
    }

    /**
     * @param widgetUid
     */
    protected String getTextBoxValue(String widgetUid) {
        return onWidget(widgetUid, "getValue");
     }

    /**
     * @param widgetUid
     */
    protected void doClickButton(String widgetUid) {
        onWidget(widgetUid, "clickButton");
        pause 500
    }

    /**
     * @param widgetUid
     */
    protected void doClickCheckBox(String widgetUid) {
        onWidget(widgetUid, "clickCheckBox");
        pause 500
    }

    /**
     * @param widgetUid
     */
    protected void doClickRadioButton(String widgetUid) {
        onWidget(widgetUid, "clickRadioButton");
        pause 500
    }

    /**
     * @param widgetName
     * @param index
     */
    protected void doSelectComboBoxItem(String widgetUid, int index) {
        onWidget(widgetUid, "clickArrowButton");
        pause 500

        onWidget(widgetUid, "selectItem", index);
        pause 500
    }

    /**
     * @param widgetName
     * @param index
     */
    protected void doSelectTooltipDialogTextBoxItem(String widgetUid, int index) {
        onWidget(widgetUid, "clickTextBox");
        pause 500

        onWidget(widgetUid, "selectItem", index);
        pause 500
    }

    /**
     * @param widgetName
     * @param input
     * @param index
     */
    protected void doSelectAutoCompleteTextBoxItem(String widgetUid, String input, int index) {
        doInputTextBox(widgetUid, input);

        onWidget(widgetUid, "selectItem", index);
        pause 500
    }

    public String getComboBoxValue(String widgetName) {
        String widgetUid = String.format("%s.%s", getModuleName(), widgetName);

        def textboxUid = String.format("%s.ComboBox", widgetUid);
        def inputBoxUid = String.format("%s.InputField.InputTextBox", textboxUid);

        return getValue(inputBoxUid);
    }

    /**
     * if optionSize=5, then range=[1, 5]
     * @param optionSize
     * @return
     */
    protected int getRandomIndex(int optionSize) {
        int index = 0;

        if (optionSize > 0) {
            int max = optionSize;
            index = RandomUtils.nextInt(max) + 1;
        }

        return index;
    }

    @Deprecated
    protected Object[] getVisibleOptionUids(String listUid, boolean onlyVisible) {
        return getVisibleOptionUids(listUid, null, onlyVisible);
    }

    @Deprecated
    protected Object[] getVisibleOptionUids(String listUid, String optionItemUid, boolean onlyVisible) {
        def options = [listUid];

        if (!isElementPresent(listUid)) {
            console.debug("not show", listUid);
            return options;
        }

        int listSize = getListSize(listUid);
        console.debug(listUid, listSize);

        for (index in 1..listSize) {
            def optionUid = String.format("%s[%d]", listUid, index);
            if (optionItemUid) {
                optionUid = String.format("%s.%s", optionUid, optionItemUid);
            }

            if (onlyVisible) {
                if (isVisible(optionUid)) {
                    options.push(optionUid);
                }
            } else {
                options.push(optionUid);
            }
        }

        return options;
    }

    @Deprecated
    protected void clickListOption(Object[] options, int index) {
        //console.debug(options);

        if (index == RANDOM_INDEX) {
            index = getRandomIndex(options.length);
            //console.debug(options[0], index);
        }

        if (index > 0 && index < options.length) {
            def optionUid = options[index];
            mouseOver optionUid
            pause 1000

            click optionUid
            pause 500
        }
    }
}
